<template>
  <div class="bar-box">
    <div class="navigation" :style="{ width: items.length * 70 + 100 + 'px' }">
      <ul :style="{ width: items.length * 70 + 80 + 'px' }">
        <li v-for="(item, index) in items" :key="index" :class="['list', { active: isActive(index) }]" @click="setActive(index)">
          <a href="#">
            <span class="icon">
              <img class="img-box" :src="item.iconSrc" alt="" />
            </span>
            <span class="text">{{ item.text }}</span>
          </a>
        </li>
        <div class="indicator"></div>
      </ul>
    </div>
  </div>
</template>

<script setup>
import { ref } from 'vue';

// 接收外部传入的 items 数据
const props = defineProps({
  items: {
    type: Array,
    required: true,
  },
});

const activeIndex = ref(0);

function isActive(index) {
  return index === activeIndex.value;
}

function setActive(index) {
  activeIndex.value = index;
  const indicator = document.querySelector('.indicator');
  indicator.style.transform = `translateX(calc(70px * ${index}))`;
}
</script>

<style scoped lang="scss">
.bar-box {
  display: flex;
  justify-content: center;
  align-items: center;
  min-height: 100vh;

  .navigation {
    position: relative;
    height: 70px;
    background: #ffffff;
    display: flex;
    justify-content: center;
    align-items: center;
    border-radius: 10px;

    ul {
      display: flex;

      li {
        position: relative;
        list-style: none;
        width: 70px;
        height: 70px;
        z-index: 1;

        a {
          position: relative;
          display: flex;
          justify-content: center;
          align-items: center;
          flex-direction: column;
          width: 100%;
          text-align: center;
          font-weight: 500;

          .icon {
            position: relative;
            display: block;
            line-height: 75px;
            font-size: 1.5em;
            text-align: center;
            transition: 0.5s;
            color: var(--clr);

            .img-box {
              width: 20px;
              height: 20px;
            }
          }

          .text {
            position: absolute;
            color: var(--clr);
            font-weight: 400;
            font-size: 0.75em;
            letter-spacing: 0.05em;
            transition: 0.5s;
            opacity: 0;
            transform: translateY(20px);
          }
        }
      }

      li.active a .icon {
        transform: translateY(-35px);
      }
      li.active a .text {
        opacity: 1;
        transform: translateY(10px);
      }

      .indicator {
        position: absolute;
        top: -50%;
        width: 70px;
        height: 70px;
        background: #3dbff6;
        border-radius: 50%;
        border: 6px solid var(--clr);
        transition: 0.5s;
      }

      .indicator::before {
        content: '';
        position: absolute;
        top: 50%;
        left: -22px;
        width: 20px;
        height: 20px;
        border-top-right-radius: 20px;
        box-shadow: 0px -10px 0 0 var(--clr);
      }

      .indicator::after {
        content: '';
        position: absolute;
        top: 50%;
        right: -22px;
        width: 20px;
        height: 20px;
        background: transparent;
        border-top-left-radius: 20px;
        box-shadow: 0px -10px 0 0 var(--clr);
      }
    }
  }
}
</style>